package com.ant.storage.service.impl;

import com.ant.storage.dto.InventoryDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.InventoryLogMapper;
import com.ant.storage.pojo.Commodity;
import com.ant.storage.pojo.InventoryLog;
import com.ant.storage.pojo.Subscribe;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.ICommodityService;
import com.ant.storage.service.IInventoryLogService;
import com.ant.storage.vo.InventoryLogVo;
import com.ant.storage.vo.PickingVo;
import com.ant.storage.vo.PurchaserPriceVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author melon
 * @since 2022-04-13
 */
@Service
public class InventoryLogServiceImpl extends ServiceImpl<InventoryLogMapper, InventoryLog> implements IInventoryLogService {


    @Autowired
    ICommodityService commodityService;

    @Resource
    InventoryLogMapper logMapper;

    @Autowired
    public ServletContext servletContext;

    @Override
    // public  PageInfo<InventoryLog> queryDetailedList(InventoryDto dto) {
    public Map<String, Object> queryDetailedList(InventoryDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper wrapper = new QueryWrapper();
            if (dto.getId() != null) {
                wrapper.eq("inventory_id", dto.getId());
            }
            if (dto.getWarehouseId() != null) {
                wrapper.eq("warehouse_id", dto.getWarehouseId());
            }
            wrapper.orderByDesc("id");
            List<InventoryLog> list = this.baseMapper.selectList(wrapper);

            list.stream().forEach(item -> {
                Commodity commodity= logMapper.getCommodityName(dto.getId()==null?item.getInventoryId():dto.getId());
                item.setCommodityName(commodity.getCommodityName());
            });

            Map<String, Object> map = new HashMap<>();
            PageInfo<InventoryLog> pageInfo = new PageInfo<>(list);
            List<InventoryLog> getList = logMapper.getTotal(dto.getId(), dto.getWarehouseId());
            if (getList.size() > 0) {
                int wct = (int) getList.stream().filter(s -> s.getStockState() == 1).count();
                int out = (int) getList.stream().filter(f -> f.getStockState() == 2).count();
                map.put("WarehousingCount", wct);
                map.put("OutboundCount", out);
            }
            map.put("data", pageInfo.getList());
            map.put("total", pageInfo.getTotal());
            return map;
        } catch (Exception e) {
            throw new AntException(e.getMessage());
        }
    }

    @Override
    public  Map<String, Object> querylist(InventoryDto dto) {
        try {
            Map<String, Object> map=new HashMap<>();
            UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
            dto.setCityId(userInfo.getCityId().toString());
            List<PickingVo> list = logMapper.getInventoryLogList(dto);
            map.put("data",list);
            map.put("total",logMapper.getInventoryCount(dto));
            return map;
        } catch (Exception e) {
            throw new AntException(e.getMessage());
        }
    }

    @Override
    public Map<String, Object> getCommodityList(InventoryDto dto) {
        try {
            Map<String, Object> map=new HashMap<>();
            UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
            Object roles =  servletContext.getAttribute("roles");
            if( roles==null || !("vadmin").equals(roles)){
                dto.setWarehouseId(userInfo.getCityId());
            }
            List<InventoryLogVo> list = logMapper.getCommodityList(dto);
            map.put("data",list);
            map.put("total",logMapper.getCommodityCount(dto));
            dto.setTypes(1);
            map.put("receiptPrice",logMapper.getReceiptPriceCount(dto));
            dto.setTypes(2);
            map.put("deliveryPrice",logMapper.getReceiptPriceCount(dto));
            return map;
        } catch (Exception e) {
            throw new AntException(e.getMessage());
        }
    }

    @Override
    public Map<String, Object> getChannelList(InventoryDto dto) {
        try {
            Map<String, Object> map=new HashMap<>();
            List<InventoryLogVo> list = logMapper.getChannelList(dto);
            map.put("data",list);
            map.put("total",logMapper.getChannelCount(dto));
            return map;
        } catch (Exception e) {
            throw new AntException(e.getMessage());
        }
    }

    @Override
    public  List<PurchaserPriceVo> findPersonalCensus(InventoryDto inventoryDto) {
        return logMapper.findPersonalCensus(inventoryDto);
    }
}
